package cn.wowjoy.pda.android_pharmacy.mvvm.presenter.staticH;

import android.content.Intent;
import android.util.Log;

import java.util.Collections;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGABindingViewHolder;
import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.PutBeenDataCompare;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.putInfo.PutTotalBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.putInfo.PutTotalNode;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.staticH.StaticPutIndexViewModel;
import cn.wowjoy.pda.android_pharmacy.ui.activity.staticH.put.StaticPutMedicineDetailActivity;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/16.
 */

public class StaticPutIndexPresenter extends BasePresenter<StaticPutIndexViewModel> {
    private ApiService mApiService;
    private MDialog waitDialog;
    @Override
    public void onCreatePresenter() {
        viewModel.wlinnerAdapter.setItemEventHandler(this);
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
//        getDataList();
    }
    public void onTestItemClickItem(BGABindingViewHolder holder, PutTotalBeen model) {
        //TODO:跳转传递数据
        Intent mIntent = new Intent(activity, StaticPutMedicineDetailActivity.class);
        mIntent.putExtra("DJJLID", model.getDJJLID());
        activity.startActivity(mIntent);
     }
    public  void getDataList(){
        //联网访问接口  获取所有待处理上架订单
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(PutTotalNode.getContent("PDAQ08","120108")))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
//                         waitDialog.dismiss();
                        //TODO:门诊信息获取失败 不跳转并提示重新扫码
                        if(!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)){
                            new MyToast(activity).showinfo("上架单信息获取失败");
                            return;
                        }
                        List<PutTotalBeen> putTotalBeenList = XmlUtils.getPutTotalBeenList(xmlData.body.roleInfoResponse.retXml);
                        if(null != putTotalBeenList && putTotalBeenList.size()!=0){
                            RxBus.getInstance().post(Contants.STATIC_PUT_INDEX_PENDING_COUNT,putTotalBeenList);
                            if(null == putTotalBeenList.get(0).getDJJLID() || putTotalBeenList.get(0).getDJJLID().equals("")){
                                putTotalBeenList.get(0).setShow(false);
                            }
                            Collections.sort(putTotalBeenList,new PutBeenDataCompare());
                            viewModel.setData(putTotalBeenList);
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
}
